package Conexion;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.net.UnknownHostException;

/**
 *
 * @author usuario
 */
public class Cliente {
        private Socket s;
        private PrintStream p;
        private BufferedReader b;
        private String host = "127.0.0.1";
        private int port = 4443;

    public void crearConex(){
        
        //BufferedReader    in = new BufferedReader(new InputStreamReader(System.in));
        try {
            //Creo una conexion al socket servidor
            s = new Socket(host,port);

            //Creo las referencias al canal de escritura y lectura del socket
            p = new PrintStream(s.getOutputStream());
            b = new BufferedReader ( new InputStreamReader ( s.getInputStream() ) );
        }catch (UnknownHostException e) {
            System.out.println("No puedo conectarme a " + host + ":" + port);
        }catch (IOException e) {
            System.out.println("Error de E/S en " + host + ":" + port);
        }
    }

    public void cerrarConex(){
        try{
            p.close();
            b.close();
            s.close();
        }catch (UnknownHostException e) {
            System.out.println("No puedo conectarme a " + host + ":" + port);
        }catch (IOException e) {
            System.out.println("Error de E/S en " + host + ":" + port);
        }

    }
    public String EnviarServer(String OP) {
            String respuesta = null;
            try{
                p.println( OP );
                respuesta = b.readLine();
                System.out.println(respuesta);
            }catch (IOException e) {
                System.out.println("Error de E/S en " + host + ":" + port);
            }
        return respuesta;
    }
}
